package com.iot.ic.service;



import com.iot.ic.common.CustomException;
import com.iot.ic.common.Options;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.dao.TeamInfoMapper;
import com.iot.ic.model.TeamInfo;
import com.iot.ic.utils.MyComparator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * Created by xiuge on 2017/2/20.
 */
@Service
public class TeamServiceImpl implements TeamService {

	@Autowired
	private TeamInfoMapper teamInfoMapper;

	@Override
	public List<TeamInfo> listTeamInfos(TeamInfo teamInfo) {		
		List<TeamInfo> result = teamInfoMapper.listTeamInfos(teamInfo);
		//Collections.sort(result, new MyComparator());
		return result;
	}

	@Override
	public int addTeam(TeamInfo teamInfo){
		isTeamNameExist(teamInfo.getTeamName());
		teamInfo.setTeamState(Options.NoStart.getCode());
		int result=teamInfoMapper.insertSelective(teamInfo);
		return  result;
	}

	@Override
	public int updateTeam(TeamInfo teamInfo){
		int result=teamInfoMapper.updateByPrimaryKeySelective(teamInfo);
		return result;
	}


	@Override
	public int deleteTeam(int teamId){
		TeamInfo teamInfo=new TeamInfo(teamId);
		teamInfo.setDelFlag(Options.Deleted.getCode());
		int result=teamInfoMapper.updateByPrimaryKeySelective(teamInfo);
		return result;
	}

	/**
	 * 判断车队名称是否已存在
	 * @param teamName
     */
	private void isTeamNameExist(String teamName){
		List<TeamInfo> teamInfos=teamInfoMapper.selectByInfos(new TeamInfo(teamName));
		if (teamInfos.size()>0)
			throw new CustomException(ReturnCode.Err_TeamNameExist);
	}

	@Override
	public TeamInfo getTeam(int teamId) {
		TeamInfo teamInfo = teamInfoMapper.selectByPrimaryKey(teamId);
		return teamInfo;
	}

	
	
	
	
	
}
